package com.danco.training.zaneuskaya.domain.service.storage;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;









import com.danco.training.zaneuskaya.cswworker.CSVReader;
import com.danco.training.zaneuskaya.cswworker.CSVSaver;
import com.danco.training.zaneuskaya.cswworker.Separator;
import com.danco.training.zaneuskaya.domain.comparator.ClaimComaratorByClaimAmount;
import com.danco.training.zaneuskaya.domain.comparator.ClaimComparatorByAlphabet;
import com.danco.training.zaneuskaya.domain.entity.Book;
import com.danco.training.zaneuskaya.domain.entity.Claim;

import org.apache.log4j.Logger;














import com.danco.training.zaneuskaya.domain.entity.storage.ClaimStorage;


public class ClaimStorageService implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4420530563741651021L;
	private ClaimStorage claimStorage;
	private BookStorageService bookStorageService = BookStorageService.getBookStorageSeviceInstance();
	
	private static final Logger logger = Logger.getLogger(ClaimStorageService.class);
	private static ClaimStorageService claimStorageServiceInstance;
	
	private ClaimComparatorByAlphabet claimComparatorByAlphabet = new ClaimComparatorByAlphabet();
	private ClaimComaratorByClaimAmount claimComaratorByClaimAmount = new ClaimComaratorByClaimAmount();
	
	

	public ClaimStorageService() {
		
		this.claimStorage = ClaimStorage.getClaimStorageInstance();
	}

	public List<Claim> getClaimList() {
		List<Claim> res = claimStorage.getClaims();
		logger.trace("Claim list");
		return res;

	}
	
	public List<Integer> getClaimListId(){
		List<Integer> res = new ArrayList<Integer>();
		for (Claim c :claimStorage.getClaims()) {
			res.add(c.getId())	;			
				}
			
		
		return res;
	
	}
	
	public int generateUnexistedId(){
		int i = 1;
		while(getClaimListId().contains(i)){
			i++;
		}
		return i;
	}
	
	
	public List<Claim> sortClaimsByAlphabet(List<Claim> claims) {
		Collections.sort(claims, claimComparatorByAlphabet);
		logger.trace("Claims are sorted by alphabet");
		return claims;

	}

	public List<Claim> sortClaimsByClaimAmount(List<Claim> claims) {
		Collections.sort(claims, claimComaratorByClaimAmount);
		logger.trace("Claims are sorted by the claim amoumt");
		return claims;
	}

	
	
	public Claim getClaimById(int id){
		for (Claim claim: claimStorage.getClaims()){
			if (claim.getId() == id){
				logger.trace("Claim is founded in Claimstorage by id");
				return claim;
			}
		}
		logger.info("Claim not founded in Claimstorage ");
		return null;
		
	}
	public boolean submitClaim(Claim claim) {
		
		
		if (getClaimById(claim.getId()) == null){
			return claimStorage.getClaims().add(claim);
			
		}
		
		else{
			logger.info("Claim not submitted");
			return false;
		}

	}
	
public boolean removeClaim(int id) {
		
	
		if (getClaimById(id) != null){
			return claimStorage.getClaims().remove(getClaimById(id));
		}
		return false;
	}


public void saveClaimListToFile(String file){
	ArrayList<String> res = new ArrayList<String>();
	for (Claim c:getClaimList()){
		res.add(entityDetails(c));
	}
	try {
		CSVSaver.saveListToFile(file,res);
	} catch (IOException e) {
		logger.error(e.getMessage(), e);
	}
}

public ArrayList<Claim> readClaims(String file){
	ArrayList<Claim> res = new ArrayList<Claim>();
	for (int i = 0; i < CSVReader.readFile(file).length; i++) {
		String claim[] = CSVReader.readFile(file)[i].split(Separator.SEPARATOR.getS());
		
		Claim c = new Claim(Integer.parseInt(claim[0]),bookStorageService.getBookById(Integer.parseInt(claim[1])), Integer.parseInt(claim[2]));
		res.add(c);
	}
	return res;
}



public static ClaimStorageService getClaimStorageServiceInstance() {
	if(claimStorageServiceInstance == null){
		claimStorageServiceInstance = new ClaimStorageService();
	}
	return claimStorageServiceInstance;
}

public BookStorageService getBookStorageService() {
	return bookStorageService;
}

public void setBookStorageService(BookStorageService bookStorageService) {
	this.bookStorageService = bookStorageService;
}


public static String entityDetails(Claim t){
	
	StringBuffer str = new StringBuffer();
	str.append(t.getId());
	str.append(Separator.SEPARATOR.getS());		
	str.append(t.getBook().getId());
	str.append(Separator.SEPARATOR.getS());
	str.append(t.getClaimAmount());
	
	return str.toString();
}

	
}
